package com.danny.demo.controller;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class AspectDemo {
    private Logger logger = LoggerFactory.getLogger(AspectDemo.class);

    //定义切入点
    @Pointcut("execution( *  com.danny.demo.controller.*.*(..))")
    public void pointCut(){

    }

    //定义前置通知
    @Before("pointCut()")
    public void before(){
        logger.info("【注解：Before】------------------切面  before");
        System.out.println("今天开学啦！");
    }

    //定义后置返回通知
    @AfterReturning(returning = "ret",pointcut = "pointCut()")
    public void afterReturing(Object ret){
        logger.info("【注解：AfterReturning】这个会在切面最后的最后打印，方法的返回值 : " + ret);
    }

    //后置最终通知，final增强，不管是抛出异常还是正常退出都会执行
    @After("pointCut()")
    public void after(){
        logger.info("【注解：After】方法最后执行.....");
    }

    //环绕通知
    @Around("pointCut()")
    public Object around(ProceedingJoinPoint pjp){
        logger.info("【注解：Around . 环绕前】方法环绕start.....");

        try {
            Object o = pjp.proceed();
            logger.info("【注解：Around. 环绕后】方法环绕proceed，结果是 :" + o);
            return o;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return null;
        }
    }
}
